package com.dhecp.core.utils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.RectangleReadOnly;
import com.itextpdf.text.pdf.Barcode128;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfUtil2 {
    
    public static void main(String[] args) throws FileNotFoundException, DocumentException {
        String FILE_DIR = "D://data";
        FileOutputStream out = new FileOutputStream(FILE_DIR + "/商品标2.pdf");
        //Step 1—Create a Document.
		Rectangle rec = new RectangleReadOnly(500, 300);
        Document document = new Document(rec);
        //Step 2—Get a PdfWriter instance.
        PdfWriter pdfWriter = PdfWriter.getInstance(document, out);
        //Step 3—Open the Document.
        document.open();
        //Step 4—Add content.
        document.add(new Paragraph("Hello World"));
        PdfContentByte cb = new PdfContentByte( pdfWriter);
        generateQRcode(cb, document);
        //Step 5—Close the Document.
        document.close();
	}


    private static void generateQRcode(PdfContentByte cb, Document document) throws DocumentException {
        String myString = "X0010CFKUL";

        Barcode128 code128 = new Barcode128();
        code128.setCode(myString.trim());
        code128.setCodeType(Barcode128.CODE128);
        Image code128Image = code128.createImageWithBarcode(cb, null, null);
        code128Image.setAbsolutePosition(10,700);
        code128Image.scalePercent(125);
        document.add(code128Image);
    }
}
